package com.cssy.xxkj.basic.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.xxkj.basic.dao.BasicProductBomMaterialsDAO;
import com.cssy.xxkj.basic.dao.BasicProductBomProcessDAO;
import com.cssy.xxkj.basic.dao.BasicSeriesDAO;
import com.cssy.xxkj.basic.entity.BasicProductBomMaterialsDO;
import com.cssy.xxkj.basic.entity.BasicProductBomProcessDO;
import com.cssy.xxkj.basic.entity.BasicSeriesDO;
import com.cssy.xxkj.basic.service.IBasicSeriesService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.impl.SyCodeService;


@Service("basicSeriesService")
public class BasicSeriesServiceImpl implements IBasicSeriesService{

	
	@Resource(name="basicSeriesDAO")	
	private BasicSeriesDAO BasicSeriesDAO;
	
	@Resource(name="basicProductBomMaterialsDAO")	
	private BasicProductBomMaterialsDAO basicProductBomMaterialsDAO;
	
	@Resource(name="basicProductBomProcessDAO")	
	private BasicProductBomProcessDAO basicProductBomProcessDAO;
	
	@Resource(name="syCodeService")
	private SyCodeService syCodeService;

	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<BasicSeriesDO>  list=BasicSeriesDAO.find(scs);
		rs.setRecordCount(BasicSeriesDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		return BasicSeriesDAO.deleteBasicSeriesDOByPrimaryKey(id);
	}

	@Override
	public Integer update(BasicSeriesDO t, User user)
			throws SyBaseException {
		t.setBizModifyName(user.getName());
		t.setBizModifyDate(new Date());
		t.setBizVersionMark(t.getBizVersion());
		t.setBizVersion(t.getBizVersion()+1);
		return BasicSeriesDAO.updateBasicSeriesDO(t);
	}

	@Override
	public BasicSeriesDO save(BasicSeriesDO t, User user)
			throws SyBaseException {
		String id=IDCodeTypeUtil.getId();
		t.setId(id);
		t.setBizCreatorName(user.getName());
		t.setBizCreatorDate(new Date());
		t.setBizVersion(1);
		BasicSeriesDAO.insertBasicSeriesDO(t);
		return t;
	}

	@Override
	public BasicSeriesDO findListById(String id)
			throws SyBaseException {
		return BasicSeriesDAO.findBasicSeriesDOByPrimaryKey(id);
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		return BasicSeriesDAO.deleteBizRemoved(id);
	}

	@Override
	public void SbomSave(HttpServletRequest request, BasicSeriesDO curAP)
			throws SyBaseException {
		try{
			String materItem =request.getParameter("materItem");
			String processItem =request.getParameter("processItem");
			//新增
			if(curAP.getId()==null||"".equals(curAP.getId())){
				curAP.setZd001Code(syCodeService.getCode("XL", "XL", SyCode.DATE_YYMMDD, 4));
				String id = IDCodeTypeUtil.getId();
				curAP.setId(id);
				this.save(curAP, SessionUtil.getUser(request));
			}else{
				int rt=0;
				rt=this.update(curAP, SessionUtil.getUser(request));
				if(0==rt){
					throw new SyBaseException("您修改的行数据发生改变！请重新打开查看");
				}
				if(materItem!=null&&!"".equals(materItem)){
					List<BasicProductBomMaterialsDO> axxlist = JSONArray.parseArray(materItem,BasicProductBomMaterialsDO.class);
					if(axxlist!=null&&!axxlist.isEmpty()&&axxlist.size()>0){
						basicProductBomMaterialsDAO.deleteBomId("XL"+curAP.getId());
						for(int i=0;i<axxlist.size();i++){
							axxlist.get(i).setId(IDCodeTypeUtil.getId());
							axxlist.get(i).setZd001BomId("XL"+curAP.getId());
							basicProductBomMaterialsDAO.insertBasicProductBomMaterialsDO(axxlist.get(i));
						}
					}
				}
				if(processItem!=null&&!"".equals(processItem)){
					List<BasicProductBomProcessDO> axxlist = JSONArray.parseArray(processItem,BasicProductBomProcessDO.class);
					if(axxlist!=null&&!axxlist.isEmpty()&&axxlist.size()>0){
						basicProductBomProcessDAO.deleteBomId("XL"+curAP.getId());
						for(int i=0;i<axxlist.size();i++){
							axxlist.get(i).setId(IDCodeTypeUtil.getId());
							axxlist.get(i).setZd001BomId("XL"+curAP.getId());
							basicProductBomProcessDAO.insertBasicProductBomProcessDO(axxlist.get(i));
						}
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
	}
	
}
